package com.proxy.dynamic.self;

import net.sf.cglib.proxy.InvocationHandler;
import net.sf.cglib.proxy.Proxy;

import java.lang.reflect.Method;

public class Factory{

    private Object machine;

    public Factory(Object machine) {
        this.machine = machine;
    }

    public Object getMachineInstance(){
        return Proxy.newProxyInstance(this.machine.getClass().getClassLoader(), this.machine.getClass().getInterfaces(), new InvocationHandler() {
            @Override
            public Object invoke(Object o, Method method, Object[] objects) throws Throwable {
                System.out.println("代理开始");
                Object invoke = method.invoke(machine, objects);
                System.out.println("代理结束");
                return invoke;
            }
        });
    }
}
